﻿var path = "/base/menu";


layui.use(['table', 'laypage'], function () {
    table = layui.table;
    layPage = layui.laypage;
    form = layui.form;

    /* 初始化表格 */
    initTable();
    // 搜索
    $.searchTable.searchBtn();

    //监听显示/隐藏操作
    form.on('switch(visibleTemplate)', function (obj) {
        updateVisible(this.value, obj.elem.checked);
    });

    //监听行工具事件
    table.on('tool(sysMenuLay)', function (obj) {
        let data = obj.data;
        if ('del' === obj.event) {
            let removeUrl = path + '/' + data.id;
            $.http.remove(removeUrl, data.title);
        } else if ('edit' === obj.event) {
            let editUrl = path + '/edit/' + data.id + "/button";
            $.modal.open('修改按钮', editUrl, '400', '320');
        }
    });

});

/* 加载表格 */
function initTable() {
    let title = $("#name").val();
    let parentId = $("#parentId").val();

    $.table.init({
        url: path + "/page",
        where: {
            title: title,
            parentId: parentId
        },
        cols: [[
            {field: 'title', title: '按钮名称', align: "center"},
            {field: 'perms', title: '权限标识', align: "center"},
            {field: 'visible', title: '按钮状态', align: "center", templet: "#visibleTemplate"},
            {field: 'createTime', title: '创建时间', align: "center"},
            {field: 'createUser', title: '创建人', align: "center"},
            {fixed: 'right', title: '操作', toolbar: '#toolbarHandle', width: 200, align: "center"}
        ]],
    })
}

/* 添加按钮 */
function saveSysMenu() {
    let id = $("#parentId").val();
    let addUrl = path + '/add/' + id + "/button";
    $.modal.open("添加按钮", addUrl, "400", "320");
}

/* 修改菜单状态 */
function updateVisible(id, visible) {
    let status = "2";
    if (visible) {
        status = '1';
    }
    let url = path + "/" + id + "/visible/" + status;
    $.http.putAjax(url, {}, function (res) {
        let code = res.status;
        // 操作成功
        if (WEB_STATUS.SUCCESS === code) {
            let msg = '显示成功';
            if (visible) {
                msg = "隐藏成功";
            }
            $.msg.msgSuccess(msg);
        } else {
            initTable();
            $.msg.msgWarning(res.msg)
        }
    })
}
